<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../dbcom/CouponCom.php");
require_once(dirname(__FILE__) . "/../dbcom/AddCpCom.php");

$couponcom = new CouponCom();
$addcpcom = new AddCpCom();

$cont = $_REQUEST['cont'];
switch ($cont) {
    case 1://删除
        $id = $_REQUEST['id'];
        $res = $couponcom->deleteCoupon($id);
        if ($res) {
            echo json_encode(array("code" => 0, "msg" => "success"));
        } else {
            echo json_encode(array("code" => 400, "msg" => "delete coupon fail"));
        }
        break;
    case 2://批量删除
        $ids = $_REQUEST['ids'];
        $idarr = explode(",", $ids);
        foreach ($idarr as $v) {
            $res = $couponcom->deleteCoupon($v);
            if (!$res) {
                echo json_encode(array("code" => 400, "msg" => "delete coupon fail"));
                exit();
            }
        }
        echo json_encode(array("code" => 0, "msg" => "success"));
        break;
    case 3://发放
        $type = $_REQUEST['type'];
        $number = $_REQUEST['number'];
        $finaltime = $_REQUEST['finaltime'];
        $totalfee = $_REQUEST['totalfee'];
        $info = $_REQUEST['info'];
        $number = (int) $number;
        $createtime = time();
        $finaltime = strtotime($finaltime);
        $ijk = 0;
        $idsarr = array();
        for ($i = 0; $i < $number; $i++) {
            $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
            $code = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
            $res1 = $couponcom->addCoupon($type, $createtime, $finaltime, $code, $info, $info, $totalfee);
            if (!$res1) {
                echo json_encode(array("code" => 400, "msg" => "add coupon fail", "data" => $ijk));
                exit();
            }
            $ijk++;
            array_push($idsarr, $res1);
        }
        $ids = implode(",", $idsarr);
        $res = $addcpcom->addCp($ijk, $totalfee, $createtime, $finaltime, $ids, $type, $info);
        if ($res) {
            echo json_encode(array("code" => 0, "msg" => "success"));
        } else {
            echo json_encode(array("code" => 400, "msg" => "add coupon_cp fail", "data" => $ijk));
        }
        break;
    case 4://
        $id = $_REQUEST['id'];
        $addcpinfo = $addcpcom->getCp($id);
        $resdata = array();
        if ($addcpinfo) {
            $resdata['type'] = $addcpinfo['type'];
            $resdata['number'] = $addcpinfo['number'];
            $resdata['final_time'] = date("Y-m-d H:i", $addcpinfo['final_time']);
            $resdata['total_fee'] = $addcpinfo['total_fee'];
            $resdata['info'] = $addcpinfo['info'];
            echo json_encode(array("code" => 0, "msg" => "success", "data" => $resdata));
        } else {
            echo json_encode(array("code" => 400, "msg" => "get addcp fail"));
        }
        break;
    case 5:
        $type = $_REQUEST['type'];
        $number = $_REQUEST['number'];
        $finaltime = $_REQUEST['finaltime'];
        $totalfee = $_REQUEST['totalfee'];
        $info = $_REQUEST['info'];
        $number = (int) $number;
        $createtime = time();
        $finaltime = strtotime($finaltime);
        $ijk = 0;
        $idsarr = array();
        for ($i = 0; $i < $number; $i++) {
            $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
            $code = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
            $res1 = $couponcom->addCoupon($type, $createtime, $finaltime, $code, $info, $info, $totalfee);
            if (!$res1) {
                echo json_encode(array("code" => 400, "msg" => "add coupon fail", "data" => $ijk));
                exit();
            }
            $ijk++;
            array_push($idsarr, $res1);
        }
        $ids = implode(",", $idsarr);
        $res = $addcpcom->addCp($ijk, $totalfee, $createtime, $finaltime, $ids, $type, $info);
        if ($res) {
            echo json_encode(array("code" => 0, "msg" => "success"));
        } else {
            echo json_encode(array("code" => 400, "msg" => "add coupon_cp fail", "data" => $ijk));
        }
        break;
    case 6://撤销
        $id = $_REQUEST['id'];
        $addcpinfo = $addcpcom->getCp($id);
        $idsarr = explode(",", $addcpinfo['ids']);
        $number = $addcpinfo['number'];
        if (!empty($idsarr)) {
            foreach ($idsarr as $v) {
                $res1 = $couponcom->deleteCoupon($v);
                if (!$res1) {
                    echo json_encode(array("code" => 400, "msg" => "delete coupon fail"));
                    exit();
                }
                $number--;
            }
        }
        $res = $addcpcom->changeNumber($number, $id);
        if ($res) {
            echo json_encode(array("code" => 0, "msg" => "success"));
        } else {
            echo json_encode(array("code" => 400, "msg" => "change coupon_cp fail"));
        }
        break;
    case 7://批量撤销
        $ids = $_REQUEST['ids'];
        $idarr = explode(",", $ids);
        if (!empty($idsarr)) {
            foreach ($idsarr as $v) {
                $addcpinfo = $addcpcom->getCp($v);
                $myidsarr = explode(",", $addcpinfo['ids']);
                $number = $addcpinfo['number'];
                if (!empty($myidsarr)) {
                    foreach ($myidsarr as $v1) {
                        $res1 = $couponcom->deleteCoupon($v1);
                        if (!$res1) {
                            echo json_encode(array("code" => 400, "msg" => "delete coupon fail"));
                            exit();
                        }
                        $number--;
                    }
                }
                $res = $addcpcom->changeNumber($number, $id);
                if (!$res) {
                    echo json_encode(array("code" => 400, "msg" => "change coupon_cp fail"));
                }
            }
        }
        echo json_encode(array("code" => 0, "msg" => "success"));
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined fail"));
        break;
}
exit();
